package archimede;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTabbedPane;

import com.itextpdf.text.Document;


public class Piantina extends JTabbedPane {

	private JButton plus = new JButton(new ImageIcon(ClassLoader.getSystemResource("plus.png")));
	private int count = 0;
	private StartingWindow st;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public Piantina(final StartingWindow st) {
		super();
		this.st = st;

		plus.setBorder(null);
		plus.setContentAreaFilled(false);
		plus.setRolloverEnabled(true);
		plus.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("plus_roll.png")));
		plus.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				addTab("                     Piantina       " + (++count) + " ", new PannelloPiantina(Piantina.this, count, st));
				setTabComponentAt(count, new ButtonTabPiantina(Piantina.this));
			}
		});
		addTab("", null);
		setTabComponentAt(count, plus);
	}

	public void setCount(int c) {
		count = c;
	}

	public int getCount() {
		return count;
	}

	public void save() {
		// System.out.println("piantina " + count);

		for (int i = 1; i <= count; i++) {
			setSelectedIndex(count);
			((PannelloPiantina) getComponentAt(i)).salva();
			// (Pannello)((JScrollPane)getSelectedComponent()).getViewport().getComponent(0)
			// System.out.println(((PannelloPiantina) getComponentAt(i))
			// .getTitle());
		}
	}

	public String print() {
		String print = "";
		print = print + "\n#" + count;
		for (int i = 1; i <= count; i++) {
			print = print + "\n#" + ((PannelloPiantina) getComponentAt(i)).print();
		}
		print = print + "\n@@Piantina";
		return print;
	}

	public void setElement(String data) {
		// System.out.println("ora " + data);
		Scanner c = new Scanner(data);
		c.nextLine();
		try {
			count = Integer.parseInt(c.nextLine().substring(1));
		} catch (Exception e) {
			count = 0;
		}
		for (int i = 1; i <= count; i++) {
			PannelloPiantina tmp = new PannelloPiantina(Piantina.this, i, st);
			Properties pc = System.getProperties();
			String homePath = pc.getProperty("user.home");
			String directoryProject = homePath + pc.getProperty("file.separator") + "ArchimedeSuper" + pc.getProperty("file.separator") + st.getCount()
					+ pc.getProperty("file.separator");
			String path = directoryProject + "piantina" + i + ".png";
			// System.out.println("dove "+path);
			tmp.setPiantina(path);

			try {
				addTab(c.nextLine().substring(1), tmp);
			} catch (Exception e) {
				addTab("P" + i + "   ", tmp);
			}

			setTabComponentAt(i, new ButtonTabPiantina(Piantina.this));
		}
		setSelectedIndex(count);
	}

	public Document pdf(Document doc) {
		for (int i = 1; i <= this.count; i++) {
			((PannelloPiantina) getComponentAt(i)).pdf(doc);
		}

		return doc;
	}
	
}
